import { createRouter, createWebHashHistory } from "vue-router";
import Login from "@/components/Login.vue";
import Home from "@/views/Home/Home.vue";
const routes = [
  { path: "/", redirect: "/Login" },
  {
    path: "/Login",
    name: "Login",
    component: Login,
  },
  {
    path: "/Home",
    name: "Home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        name: "welcome",
        component: () => import("@/components/welcome.vue"),
      },
      {
        path: "/users",
        name: "User",
        component: () => import("@/views/User/Users.vue"),
      },
      {
        path: "/rights",
        name: "rights",
        component: () => import("@/views/Power/rights.vue"),
      },
      {
        path: "/roles",
        name: "roles",
        component: () => import("@/views/Power/roles.vue"),
      },
      {
        path: "/categories",
        name: "categories",
        component: () => import("@/views/Goods/categories.vue"),
      },
      {
        path: "/params",
        name: "params",
        component: () => import("@/views/Goods/Params.vue"),
      },
      {
        path: "/goods",
        name: "goods",
        component: () => import("@/views/Goods/goods.vue")
      }
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === "/Login") {
    //如果去到的时登陆页面直接放行，因为登录页不需要拦截
    return next();
  } else {
    //获取token
    const token = window.sessionStorage.getItem("token");
    if (!token) {
      return next("/Login");
    } else {
      next();
    }
  }
});

export default router;
